package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 根据分类id ，获取平台属性
 */
@RequestMapping("/admin/product")
@RestController
public class BaseInfoValueController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    /**
     * 根据id 获取属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable("category1Id")Long category1Id,
                                  @PathVariable("category2Id")Long category2Id,
                                  @PathVariable("category3Id")Long category3Id){
        List<BaseAttrInfo> infos = baseAttrInfoService.getAttrInfoAndValueList(category1Id,category2Id,category3Id);
        return Result.ok(infos);
    }


    /**
     * 添加平台属性 与修改
     * @param info
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result addsaveAttrInfo(@RequestBody BaseAttrInfo info){
        baseAttrInfoService.addsaveAttrInfo(info);
        return Result.ok();
    }


    /**
     * 根据平台属性id获取到属性的所有值
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        List<BaseAttrValue> list = baseAttrValueService.list(new QueryWrapper<BaseAttrValue>().eq("attr_id", attrId));
        return Result.ok(list);
    }


}
